package org.vectrics.graphics;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;

import org.apache.log4j.Category;
import javax.naming.InitialContext;
import org.vectrics.VectricsException;
import org.vectrics.SystemException;
import org.vectrics.common.graphics.Graphic;
import org.vectrics.struts.HttpRequestConverter;
import org.vectrics.struts.Request;

import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;



public class DynaGraphicsServlet extends HttpServlet implements DynaGraphicClient {
    private static final String CONTENT_TYPE = "text/html";
    private static Category log = Category.getInstance(DynaGraphicsServlet.class);
    private boolean initialized = false;
    private static DynaGraphicsManager manager = null;
    private String configFileName = null;
    
    /**Initialize global variables*/
    public void init() throws ServletException {
    	super.init();
        log.debug("DynaGraphicsServlet initializing.");
		//ApplicationStarter starter = (ApplicationStarter)SpringApplication.getInstance()
		//	.getBeanFactory().getBean("application.initialize.class");

		//starter.initializeWebApplication(this.getServletContext());
        initializeDynaGraphicsManager();
        initialized = true;
    }
    
    /**
     * 
     *
     */
    public synchronized void initializeDynaGraphicsManager() {
        if (manager == null) {
            manager = new DynaGraphicsManager();
            manager.loadDefinitions(configFileName);
        }
    }

    /**
     * Returns the cache directory used by this servlet.  If this servlet is used for images, then
     * the configuration file must have a resources directory.
     */
    public String getCacheDirectory() {
    	return manager.getDefaultConfigurationCacheDirectory();
    }
    
    
    /**Process the HTTP Get request*/
    public void service(HttpServletRequest httpRequest, HttpServletResponse response) throws ServletException, IOException {
    	log.debug("service() - handling request for dyna graphic");
    	Request request = HttpRequestConverter.convertRequest(httpRequest);
    	manager.service(null, this, request, response);
    }


}